package com.study.d012Components;

import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

/**
 * Group容器的使用
 *
 */
public class SceneMain2 extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		Group group = new Group();
		Button b1 = new Button("b1");
		Button b2 = new Button("b2");
		Button b3 = new Button("b3");

		b1.setLayoutX(0);
		b1.setLayoutY(10);

		b2.setLayoutX(100);
		b2.setLayoutY(10);


		b3.setLayoutX(200);
		b3.setLayoutY(10);

		group.getChildren().addAll(b1,b2,b3);


		//移除组件
		//group.getChildren().remove(b1);

		//移除所有组件
		//group.getChildren().clear();

		//group 添加监听
		group.getChildren().addListener(new ListChangeListener<Node>() {

			@Override
			public void onChanged(Change<? extends Node> c) {
				System.out.println("当前节点个数：" + c.getList().size());
			}

		});



		//遍历组件
		for(Object o: group.getChildren().toArray()){
			Button button = (Button)o;
			button.setPrefWidth(150);
		}

		//b1按钮添加点击事件
		b1.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				Button b4 = new Button("b4");
				b4.setLayoutX(300);
				b4.setLayoutY(10);
				group.getChildren().add(b4);

			}
		});

		Scene scene = new Scene(group);
		stage.setScene(scene);
		stage.setWidth(500);
		stage.setHeight(400);
		stage.show();
	}
}
